import { login, logout, getInfo, register } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  // namespaced: true,
  state: {
    token: getToken(), // 登录 token（从 cookie/localStorage 读取）
    name: '', // 用户名
    avatar: '', // 头像
    userId: '', // 用户 ID
    loginFormVisible: false, // 登录弹窗显示状态
    registerFormVisible: false, // 注册弹窗显示状态
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_LOGINFORMVISIBLE: (state, loginFormVisible) => {
      state.loginFormVisible = loginFormVisible
    },
    SET_REGISTERFORMVISIBLE: (state, registerFormVisible) => {
      state.registerFormVisible = registerFormVisible
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then(res => {
            setToken(res.token)
            commit('SET_TOKEN', res.token)
            commit('SET_LOGINFORMVISIBLE', false)
            resolve()
          })
          .catch(error => reject(error))
      })
    },

    // 注册
    Register({ commit }, userInfo) {
      const data = {
        username: userInfo.username.trim(),
        password: userInfo.password,
        code: userInfo.code,
        uuid: userInfo.uuid,
      }
      return new Promise((resolve, reject) => {
        register(data)
          .then(res => resolve(res))
          .catch(error => reject(error))
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then(res => {
            const user = res.user
            const avatar = user.avatar
              ? user.avatar
              : 'https://simgbb.com/avatar/R4r8TP4965Rj.jpg'
            commit('SET_NAME', user.username) // 注意：字段名和后端保持一致
            commit('SET_AVATAR', avatar)
            commit('SET_USERID', user.id)
            resolve(res)
          })
          .catch(error => reject(error))
      })
    },

    // 显示登录/注册弹窗
    showLoginForm({ commit }) {
      commit('SET_LOGINFORMVISIBLE', true)
    },
    showRegisterForm({ commit }) {
      commit('SET_REGISTERFORMVISIBLE', true)
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit('SET_TOKEN', '')
            removeToken()
            resolve()
          })
          .catch(error => reject(error))
      })
    },

    // 前端登出
    FedLogOut({ commit }) {
      commit('SET_TOKEN', '')
      removeToken()
    },
  },
}

export default user
